<?php
class Backend_Blocks_Controller extends Backend_Controller_Crud_Vc{

    public function listAction(){
        
        $pager = Request::post('pager', 'array', array());
        $query = Request::post('search', 'string', false);
     
        $result = array( 'success'=>false, 'count'=>0, 'data'=>array());
    
        $dataModel = Model_Blocks::getInstance();
        $vc = Model_Vc::getInstance();
        
        $fields = array(
            'id' ,
            'title',
            'date_created',
            'published' , 
            'published_version',
            'date_updated'
        );

        $data = $dataModel->getListVc($pager , array('is_system'=>0), $query, $fields,'author','updater');
        
        if(empty($data))
            Response::jsonSuccess($result);
    
        $ids = array(); 
        foreach ($data as $k=>$v)
            $ids[] = $v['id'];
           
        $maxRevisions = $vc->getLastVersion('blocks',$ids);
        
        foreach ($data as $k=>&$v){
            if(isset($maxRevisions[$v['id']]))
                $v['last_version'] = $maxRevisions[$v['id']];
            else
                $v['last_version'] = 0;   
        } unset($v);
        
        $result = array(
            'success'=>true,
            'count'=>$dataModel->getCount($pager , array('is_system'=>0) , $query),
            'data'=>$data
        );
        Response::jsonArray($result);
    }
}